/**
 * 
 */
package com.yidu.utils;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

/**
 * 鎻忚堪:
 * @author liandyao
 * @date 2019骞�6鏈�17鏃� 涓婂崍8:51:53
 * @version 1.0
 */
public class EmailTools {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		sendEmail("943623303@qq.com","浣犲ソ,涓栫晫","鎴戞槸鏉ヨ嚜铏氭嫙涓栫晫鐨勪唬鐮�");
	}
 
	/**
	 * 鍙戦�侀偖浠剁殑浠ｇ爜
	 * @param email
	 * @param subject
	 * @param content
	 */
	public static void sendEmail(String email,String subject,String content) {
		try {
			//鏂板缓涓�涓厤缃被
			Properties prop = new Properties();
			//闇�瑕佸彂閫佺殑閭欢鏈嶅姟鍣�
			prop.setProperty("mail.host", "smtp.qq.com");
			//閭欢鍙戦�佸崗璁畇mtp锛屼笉鐭ラ亾鐨勮鑷鐧惧害,濡傛灉瀹炲湪鐪嬩笉鎳備篃娌″叧绯�,鍙渶瑕佹寜鐓ц繖涓啓
			prop.setProperty("mail.transport.protocol", "smtp");
			//QQ閭鐨凷SL瀹夊叏楠岃瘉
			String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

			prop.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);

			prop.setProperty("mail.smtp.socketFactory.fallback", "false");
			//QQ閭绔彛鍙�
			prop.setProperty("mail.smtp.port", "465");

			prop.setProperty("mail.smtp.socketFactory.port", "465");

			prop.put("mail.smtp.ssl.enable","true");

			//璁剧疆smtp鎺堟潈锛宼rue鏍囪瘑鍏佽
			prop.setProperty("mail.smtp.auth", "true");
			//瑙ｆ瀽鏉ュ紑濮嬫寮忓啓鍙戦�侀偖浠朵唬鐮�
			//棣栧厛闇�瑕佸垱寤簊ession,姝ession鏍囪瘑閭欢閲岄潰鐨勪細璇�,javax.mail.Session
			Session session = Session.getInstance(prop);

			//2.閫氳繃session寰楀埌transport瀵硅薄,杩欎釜瀵硅薄鏄笓闂ㄧ敤鏉ュ彂閫侀偖浠剁殑瀵硅薄
			Transport trans = session.getTransport();

			//3.鎻愪緵鐢ㄦ埛鍚嶅拰瀵嗙爜 ,杩欓噷鍐欒嚜宸辩殑QQ璐﹀彿鍜岄偖绠辩粰浣犵殑鍔ㄦ�佸瘑鐮�
			trans.connect("smtp.qq.com", "1434717598", "crzzziofqnbbhdhd");
			//4.鍒涘缓閭欢
			Message msg = new MimeMessage(session);
			//鍙戦�佽��
			msg.setFrom(new InternetAddress("1434717598@qq.com"));
			//鎺ュ彈鑰�
			msg.setRecipient(RecipientType.TO, new InternetAddress(email));
			//鏍囬
			msg.setSubject(subject);

			//閭欢鐨勬鏂�,杩欓噷鍙互涓昏鍐欎綘鐨勬湇鍔″櫒鍦板潃 锛� 渚嬪 http://localhost:8080/xxx/sid=婵�娲昏处鍙�
			//msg.setContent("<a href='http://www.baidu.com'>璇风偣鍑昏繖閲屾縺娲昏处鍙�</a>", "text/html;charset=UTF-8");
			msg.setContent(content, "text/html;charset=UTF-8");

			//5.鍙戦�侀偖浠�
			trans.sendMessage(msg, msg.getAllRecipients());

			//6.鍏抽棴鍙戦�佸璞�
			trans.close();

			System.out.println("发送邮箱成功");
		}catch (Exception e) {
			e.printStackTrace();
		}
	}

}
